// import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/init/SFHomePage.dart';
import 'package:jyofservice/init/Welcome_page.dart';
import 'package:fluro/fluro.dart';
import 'package:jyofservice/fluro/Routes.dart';
import 'package:jyofservice/fluro/Application.dart';
import 'package:mobpush_plugin/mobpush_plugin.dart';
import 'package:mobcommonlib/mobcommonlib.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'dart:io';
import 'package:jyofservice/init/LoginPage.dart';
import 'package:jyofservice/init/HomePage.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:jyofservice/util/ChineseCupertinoLocalizations.dart';
import 'package:webview_flutter_plus/webview_flutter_plus.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await LocalHostServer.start();
  await FlutterDownloader.initialize(debug: true);
  // AMapFlutterLocation.setApiKey(
  //     "3df12515e3585ae24b4203ce98ad2123", "559b552c800c23818f8259b87ee4fa93");
  FluroRouter router = FluroRouter();
  Routes.configureRoutes(router);
  Application.router = router;
  init().then((onValue) {
    MobpushPlugin.updatePrivacyPermissionStatus(true);
    Mobcommonlib.submitPolicyGrantResult(true, null);
    MobpushPlugin.getRegistrationId().then((Map<String, dynamic> ridMap) {
      print(ridMap);
      String regId = ridMap['res'].toString();
      print('------>#### registrationId: ' + regId);
    });
    if (onValue != null) {
      if (Platform.isAndroid) {
        SystemUiOverlayStyle systemUiOverlayStyle =
            SystemUiOverlayStyle(statusBarColor: Colors.transparent);
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
        // 强制竖屏
        SystemChrome.setPreferredOrientations(
            [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
      } else if (Platform.isIOS) {
        // 开发环境 false, 线上环境 true
        MobpushPlugin.setAPNsForProduction(true);
        MobpushPlugin.setCustomNotification();
        MobpushPlugin.clearBadge();
      }

      initkey().then((onValue) {
        if (onValue == null) {
          runApp(MyApp(home: LoginPage()));
        } else {
          if (onValue == 'sfjc') {
            runApp(MyApp(home: SFHomePage()));
          } else {
            runApp(MyApp(home: HomePage()));
          }
        }
      });
    }
  });
}

Future init() async {
  SharedPreferences preferences = await SharedPreferences.getInstance();
  var basuUrl = preferences.getString("baseurl");
  if (basuUrl == null || basuUrl.length == 0) {
    preferences.setString("baseurl", WebConfig().baseUrl);
    basuUrl = preferences.getString("baseurl");
  }
  return basuUrl;
}

Future initkey() async {
  SharedPreferences preferences = await SharedPreferences.getInstance();
  var basuUrl = preferences.getString("key");
  return basuUrl;
}

class MyApp extends StatelessWidget {
  final Widget? home;

  MyApp({Key? key, this.home}) : super(key: key);

  List<Locale> an = [
    const Locale('zh', 'CH'),
    const Locale('en', 'US'),
  ];
  List<Locale> ios = [
    const Locale('en', 'US'),
    const Locale('zh', 'CH'),
  ];
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(750, 1334),
      child: GestureDetector(
        onTap: (){
          FocusScopeNode focusScopeNode = FocusScope.of(context);
          if(!focusScopeNode.hasPrimaryFocus &&  focusScopeNode.focusedChild != null) {
            FocusManager.instance.primaryFocus!.unfocus();
          }
        },
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          title: '客服语音',
          onGenerateRoute: Application.router.generator,
          theme: ThemeData(
            //          primarySwatch: Colors.blue,
            //顶部导航栏和状态栏的颜色修改成黑色或者白色，需要用到这个属性
            primaryColor: Colors.blue,
            appBarTheme: AppBarTheme(
                iconTheme: IconThemeData(color: Colors.white),
                titleTextStyle: TextStyle(color: Colors.white),
                backgroundColor: Colors.blue),
          ),
          home: home,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: Platform.isIOS ? ios : an,
        ),
      ),
    );
  }
}
